package service.facade.user;

import dao.inte.CmsPageRepository;
import domain.cms.AbstractPage;
import domain.cms.CmsHomePage;
import domain.cms.CmsPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import service.ConfigurationService;
import service.Searcher;

import javax.persistence.NoResultException;
import java.util.List;

@Transactional
public class CmsUserServiceImpl implements CmsUserService {


    @Autowired(required = true)
    CmsPageRepository cmsPageRepository;
    @Autowired(required = true)
    Searcher searcher;
    @Autowired(required = true)
    private ConfigurationService configurationService;

    public CmsUserServiceImpl() {
    }


    public List<? extends AbstractPage> search(String fulltextQuery) {
        return searcher.search(fulltextQuery);
    }

    public List<CmsPage> getSections() {
        return cmsPageRepository.getPublishedSections();
    }

    public CmsPage getCmsPage(Long id) {
        return cmsPageRepository.findById(id);
    }

    public CmsHomePage getCmsHomePage() {
        return configurationService.getConfig().getCmsHomePage();
    }

    @Transactional(readOnly = true, noRollbackFor = NoResultException.class)
    public CmsPage getCmsPageByUrl(String url) {
        try {
            return cmsPageRepository.findByUrl(url);
        } catch (Exception e) {

            return null;
        }
    }


}
